import { Component, OnInit } from '@angular/core';
import { MenuService } from 'sparrow-permission-lib';

@Component({
  selector: 'app-root',
  templateUrl: './app.component.html',
  styleUrls: ['./app.component.css'],
})
export class AppComponent implements OnInit {
  isCollapsed = false;
  public constructor(private menuService: MenuService) {}

  isLogin: boolean= false
  username: string =''

  myMenus?: any[];
  ngOnInit(): void {
    console.log(sessionStorage.getItem("token"))
    this.isLogin = (sessionStorage.getItem("token")!=undefined && sessionStorage.getItem("token")!='')?true:false
    this.username = sessionStorage.getItem('username')===undefined?'':sessionStorage.getItem('username')!
    this.menuService
      .getTree('MY', undefined, undefined, '')
      .subscribe((res) => {
        this.buildMenuTreeLevel(res, 0);
        this.myMenus = res.children;
      });
  }

  buildMenuTreeLevel(menu: any, level: number) {
    menu.level = level;
    if (menu.children.length > 0) {
      menu.children.forEach((f: { level: number }) => {
        f.level = level + 1;
        this.buildMenuTreeLevel(f, f.level);
      });
    } else delete menu.children;
  }

  logout(){
    console.log(sessionStorage.getItem("token"))
    sessionStorage.removeItem('token')
    sessionStorage.removeItem('username')
    window.location.href = '/'
  }
}
